<template>
  <el-dialog
    v-model="visible"
    title="来料检验"
    width="500px"
    @close="closeDialog"
  >
    <el-form :data="state.form">
      <el-form-item label="报检数量">
        <el-input-number
          v-model="state.form.qty"
          :controls="false"
          :max="state.rowData.kyQty"
          :min="0"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="toCancel">取消</el-button>
        <el-button type="primary" @click="toConfirm()">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
  import { lailiaoReportInspectApi } from '@/api/qm/index.ts'
  import { localStorageConfig } from '@/config/localstorage.config'
  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const $baseMessage = inject('$baseMessage'),
    $baseLoading = inject('$baseLoading')
  const state = reactive({
    form: {},
    rowData: null,
  })
  const visible = ref(false)
  const toConfirm = () => {
    toJY()
  }

  const toJY = async () => {
    if (state.form.qty <= 0) {
      $baseMessage('数据有误！', 'error')
      return
    }
    const params = {
      inspectType: 3,
      sourcePk: state.rowData.pkMesMaterialH,
      pkCorp,
      pkInv: state.rowData.pkInv,
      qty: state.form.qty,
    }
    const load = $baseLoading()

    try {
      const res = await lailiaoReportInspectApi(params)
      console.log('lailiaoReportInspectApi', res)
      $baseMessage(res.msg || '操作成功!', 'success')
      load.close()
      _emits('doRefreshData')
      toCancel()
    } catch (error) {
      load.close()
    }
  }
  const toCancel = () => {
    visible.value = false
  }
  const closeDialog = () => {
    state.form.qty = 0
  }
  const handleOpen = (rowData) => {
    console.log('handleOpen', rowData)
    visible.value = true
    state.rowData = rowData
  }
  defineExpose({
    handleOpen,
  })
  const _emits = defineEmits(['doRefreshData'])
</script>
